<?php
require_once 'config.php';
class Dbi
{
	public $mysqli;
	public $error;
	public $showError = SERVICE_SHOW_ERROR;
	public $showsql = SERVICE_SHOW_SQL;
	
	function __construct($database_name = NULL){
		if(empty($database_name)){
			$database_name = DATABASE;
		}
		
		$this->mysqli = @new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, $database_name);
		
		//test whether connection OK
		if(mysqli_connect_errno()){
			$this->mysqli = FALSE;
			$this->error = new VOSqlError();
			$this->error->error = TRUE;
			$this->error->sql = "No Connection!";
			$this->error->errorDesc = mysqli_connect_error();
		}
	}
	
	function __destruct(){
		$this->close();
	}
	
	function close(){
		if($this->mysqli){
			$this->mysqli->close();
			$this->mysqli = FALSE;
		}
	}
	
	function fetchObjects($query, $className = NULL){
		if($result = $this->mysqli->query($query)){
			if($result->num_rows){
				if(empty($className)){
					while($row = $result->fetch_object()){
						$ret[] = $row;
					}
				} else {
					while($row = $result->fetch_object($className)){
						$ret[] = $row;
					}
				}
			} else {
				$ret = FALSE;
			}
		} else {
			$ret = ErrorManager::sqlError($this->mysqli, $query);
		}
		return $ret;
	}
	
	function fetchSingleItem($query){
		if($result = $this->mysqli->query($query)){
			if($row = $result->fetch_array()){
				$result->close();
				$ret = $row[0];
			} else {
				$ret = FALSE;
			} 
		} else {
			$ret = ErrorManager::sqlError($this->mysqli, $query);
		}
		return  $ret;
	}
	
	function fetchTableObjects(VOTableList $param, $query, $className = NULL){
		$page = $param->page;
		$limit = $param->limit;
		$totalRecord = $param->totalRecord;
		
		if(empty($page)){
			$page = 1;
		}
	
		if(empty($limit)){
			$limit = MINIMUM_LIST_LIMIT;
		}
		
		$totalPage = ceil($totalRecord / $limit);
		$startPage = $limit * $page - $limit;
		$newQuery = $query." LIMIT $startPage, $limit";
		
		if(empty($className)){
			$data = $this->fetchObjects($newQuery);
		} else {
			$data = $this->fetchObjects($newQuery, $className);
		}
		
		$result = new VOTableList();
		$result->data = $data;
		$result->limit = $limit;
		$result->page = $page;
		$result->totalPage = $totalPage;
		$result->totalRecord = $totalRecord;
		
		return $result;
	}
	
	function execute($query){
		if($this->mysqli->real_query($query)){
			$result = new VOCrudResult();
			$result->result = TRUE;
			$result->insertedId = $this->insertId();
			$ret = $result;
		} else {
			$ret = ErrorManager::sqlError($this->mysqli, $query);
		}
		return $ret;
	}
	
	
	/*
	 * CRUD - need session to execute
	 */
	
	function fetchObjectsCRUD(VOUserSession $userSession = null, $query, $className = NULL){
		if(empty($userSession)){
			return ErrorManager::sessionError();
		}

		if($this->checkSession($userSession)){
			$ret = $this->fetchObjects($query, $className);
		} else {
			$ret = FALSE;
		}
		
		return $ret;
	}
	
	function fetchSingleItemCRUD(VOUserSession $userSession = NULL, $query){
		if(empty($userSession)){
			return ErrorManager::sessionError();
		}
		
		if($this->checkSession($userSession)){
			$ret = $this->fetchSingleItem($query);
		} else {
			$ret = FALSE;
		}
		
		return $ret;
	}
	
	function fetchTableObjectsCRUD(VOUserSession $userSession = NULL, VOTableList $param, $query, $className = NULL){
		
		if(empty($userSession)){
			return ErrorManager::sessionError();
		}
		
		if($this->checkSession($userSession)){
			$ret = $this->fetchTableObjects($param, $query, $className);
		} else {
			$ret = FALSE;
		}
		
		return $ret;

	}
	
	function executeCRUD(VOUserSession $userSession = NULL, $query){
		//check if session not set
		if(empty($userSession)){
			return ErrorManager::sessionError();
		}
		
		if($this->checkSession($userSession)) {
			if($this->mysqli->real_query($query)){
				$result = new VOCrudResult();
				$result->result = TRUE;
				$result->insertedId = $this->insertId();
				$ret = $result;
			} else {
				$ret = ErrorManager::sqlError($this->mysqli, $query);
			}
		} else {
			$ret = FALSE;
		}
		return $ret;
	}
	
	function insertId(){
		return $this->mysqli->insert_id;
	}
	
	function printError($errorText){
		if($this->showError){
			$ret = $errorText;
		}
		return $ret;
		
	}
	
	function printSql($query){
		if($this->showsql){
			$ret = $query;
		}
		return $ret;
	}
	
	function checkSession(VOUserSession $userSession){
		$query = "SELECT userSecretToken FROM user_sessions 
				  WHERE 
				  userSecretToken = '$userSession->userSecretToken' AND
				  userId = $userSession->userId";
		$result = $this->fetchObjects($query);
		return $result;
	}
}
?>